package com.tuyan.aixiang.common;

import com.tuyan.aixiang.common.lang.Consumer;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class BasePresenter<V> implements IPresenter<V> {

    CompositeDisposable compositeDisposable = new CompositeDisposable();
    private CompositeSubscription mCompositeSubscription = new CompositeSubscription();

    protected WeakReference<V> iView;

    @Override
    public void register(V view) {
        iView = new WeakReference(view);
    }

    @Override
    public void unRegister() {
        compositeDisposable.dispose();
        mCompositeSubscription.unsubscribe();
        iView.clear();
    }

    @Override
    public void addDisposable(Disposable disposable) {
        if(null == disposable) return;
        compositeDisposable.add(disposable);
    }

    @Override
    public void loadImage(String uuid, Consumer<String> consumer) {
        consumer.accept(Constant.getResUrl(uuid));
//        Disposable disposable = HttpClient.getPub().res(uuid).compose(RxSchedulers.io_main())
//                .compose(ResultTransformer.handleResult())
//                .subscribe(r -> {
//                    if(null == r.get()) {
//                        consumer.accept(null);
//                        return;
//                    }
//                    consumer.accept(r.get().getUrl());
//                }, t-> {
//                    consumer.accept(null);
//                });
//        addDisposable(disposable);
    }

    /**
     * 添加Rx订阅者
     *
     * @param s 订阅者
     */
    private void addSubscription(Subscription s) {
        mCompositeSubscription.add(s);
    }

    /**
     * 订阅
     *
     * @param observable 观察对象
     * @param subscriber 订阅者
     */
    public <T> void subscribe(Observable<T> observable, final Subscriber<T> subscriber) {
        Subscription subscription = observable.subscribe(subscriber);
        addSubscription(subscription);
    }

    /**
     * 订阅
     *
     * @param observable 观察对象
     * @param actionOnNext 动作
     */
    public <T> void subscribe(Observable<T> observable, Action1<T> actionOnNext) {
        Subscription subscription = observable.subscribe(actionOnNext);
        addSubscription(subscription);
    }

    /**
     * 订阅
     *
     * @param observable 观察对象
     * @param actionOnNext 动作
     * @param actionError  error
     */
    public <T> void subscribe(Observable<T> observable, Action1<T> actionOnNext,Action1<Throwable> actionError) {
        Subscription subscription = observable.subscribe(actionOnNext,actionError);
        addSubscription(subscription);
    }
    /**
     * 订阅（走异步处理）
     *
     * @param observable 观察对象
     * @param subscriber 订阅者
     */
    public <T> void subscribeAsync(Observable<T> observable, final Subscriber<T> subscriber) {
        subscribe(observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()), subscriber);
    }


    /**
     * 订阅（走异步处理）
     *
     * @param observable 观察对象
     * @param actionOnNext   动作
     */
    public <T> void subscribeAsync(Observable<T> observable, Action1<T> actionOnNext, Action1<Throwable> actionError) {
        subscribe(observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()), actionOnNext,actionError);
    }

}
